package com.bloody.android;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ScaleDrawable;
import android.util.Log;
import android.view.GestureDetector.OnGestureListener;
import android.os.Bundle;
import android.view.Display;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class CharactersSelection extends Activity implements OnClickListener, OnGestureListener{
	
	private ImageView img;
	
	private ImageButton imgButton1;
	private ImageButton imgButton2;
	private ImageButton imgButton3;
	
	private Button button;
	
	private GestureDetector gesture;
	
	private int indexImage=0;
	
	private boolean create;
	
	private ArrayList<Integer> selectables = new ArrayList<Integer>();
	private int[] selecteds = {-1, -1, -1};
	private HashMap<Integer,String> names = new HashMap<Integer, String>();
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.characters_selection);
        Resources r = this.getApplicationContext().getResources();
        
        selectables.add(R.drawable.ichigo);
        selectables.add(R.drawable.gaara);
        selectables.add(R.drawable.naruto);
        selectables.add(R.drawable.kakashi);
        selectables.add(R.drawable.mustang);
        
        names.put(selectables.get(0), "ichigo");
        names.put(selectables.get(1), "gaara");
        names.put(selectables.get(2), "naruto");
        names.put(selectables.get(3), "kakashi");
        names.put(selectables.get(4), "mustang");
        
        gesture = new GestureDetector(this);
        
        img = (ImageView)findViewById(R.id.imgCharacter);
        img.setImageDrawable(r.getDrawable(selectables.get(indexImage)));
        img.setOnClickListener(this);
        
        imgButton1 = (ImageButton)findViewById(R.id.imgButton_characters_selection_1);
        imgButton1.setOnClickListener(this);
        imgButton2 = (ImageButton)findViewById(R.id.imgButton_characters_selection_2);
        imgButton2.setOnClickListener(this);
        imgButton3 = (ImageButton)findViewById(R.id.imgButton_characters_selection_3);
        imgButton3.setOnClickListener(this);
        
        button = (Button)findViewById(R.id.done_characters_selection);
        button.setOnClickListener(this);
        
        Bundle info = this.getIntent().getExtras();
        create = info.getBoolean("create");
	}

	@Override
	public void onClick(View v) {
		Resources r = this.getApplicationContext().getResources();
		Display display = getWindowManager().getDefaultDisplay(); 
		int width = display.getWidth();
		Drawable d = r.getDrawable(selectables.get(indexImage));
		Bitmap img = Bitmap.createBitmap((int)(width/4), (int)(width/4), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(img);
		d.setBounds(0, 0, (int)(width/4), (int)(width/4));
		d.draw(canvas);
		if(v == this.img){
			int res = add();
			if(res==0)
				imgButton1.setImageBitmap(img);
			else if(res==1)
				imgButton2.setImageBitmap(img);
			else if(res==2)
				imgButton3.setImageBitmap(img);
		}
		else if (v == imgButton1){
			imgButton1.setImageBitmap(img);
			swap(0);
		}
		else if (v == imgButton2){
			imgButton2.setImageBitmap(img);
			swap(1);
		}
		else if (v == imgButton3){
			imgButton3.setImageBitmap(img);
			swap(2);
		}
		
		if (imgButton1.getDrawable()!=null && imgButton2.getDrawable()!=null && imgButton3.getDrawable()!=null){
			button.setVisibility(0);
		}
		
		if (v == button){
			Bundle bundle = new Bundle();
			ArrayList<String> characters = new ArrayList<String>();
			for (int i=0; i<selecteds.length ; i++){
				characters.add(names.get(selecteds[i]));
			}
	    	//leur position : ligne
			if (create){
		    	ArrayList<Integer> charactersRow = new ArrayList<Integer>();
		    	charactersRow.add(4);
		    	charactersRow.add(6);
		    	charactersRow.add(8);
		    	//leur position : colonne
		    	ArrayList<Integer> charactersCol = new ArrayList<Integer>();
		    	charactersCol.add(2);
		    	charactersCol.add(1);
		    	charactersCol.add(2);
		    	//on place tout dans le bundle
		    	bundle.putStringArrayList("myCharacters", characters);
		    	bundle.putIntegerArrayList("myCharactersRow", charactersRow);
		    	bundle.putIntegerArrayList("myCharactersCol", charactersCol);
			}
			else{
				ArrayList<Integer> charactersRow = new ArrayList<Integer>();
		    	charactersRow.add(4);
		    	charactersRow.add(6);
		    	charactersRow.add(8);
		    	//leur position : colonne
		    	ArrayList<Integer> charactersCol = new ArrayList<Integer>();
		    	charactersCol.add(8);
		    	charactersCol.add(9);
		    	charactersCol.add(8);
		    	//on place tout dans le bundle
		    	bundle.putStringArrayList("myCharacters", characters);
		    	bundle.putIntegerArrayList("myCharactersRow", charactersRow);
		    	bundle.putIntegerArrayList("myCharactersCol", charactersCol);
			}
	    	Intent mIntent = new Intent();
			mIntent.putExtras(bundle);
			setResult(RESULT_OK, mIntent);
			Log.d("DEBUG", "finish");
			finish();
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event){
		return gesture.onTouchEvent(event);		
	}
	
	@Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		Resources r = this.getApplicationContext().getResources();
		
		int s = selectables.size();
		if (distanceX>=20){
        	indexImage = (indexImage + 1)%s;
        	img.setImageDrawable(r.getDrawable(selectables.get(indexImage)));
        }
        else if (distanceX<=-20){
        	indexImage = (indexImage + s - 1)%s;
        	img.setImageDrawable(r.getDrawable(selectables.get(indexImage)));
        }
		return true;
    }

	@Override
	public boolean onDown(MotionEvent arg0) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void onLongPress(MotionEvent e) {
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	private int add()
	{
		int i;
		for(i=0 ; i<3 && selecteds[i]!=-1 ; i++);//i<3 && indexes.get(i)!=null ; i++);
		
		if(i<3)
			swap(i);
		return i;
	}
	
	private void swap(int b)
	{
		int tmp = selecteds[b];
		selecteds[b]=selectables.get(indexImage);
		selectables.remove((int)indexImage);
		
		if(tmp != -1)
			selectables.add(tmp);
		
		indexImage = selectables.size()-1;
		img.setImageDrawable(this.getApplicationContext().getResources().getDrawable(selectables.get(indexImage)));
	}
}
